import { getBusinessTypeList } from '../../../../api/index'

Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    type: {
      type: String,
      value: ''
    },
    businessType: {
      type: String,
      value: ''
    }
  },
  observers: {},
  data: {
    currentItem: {},
    list: []
  },
  methods: {
    beforeOpen () {
      this.getBusinessTypeList()
    },
    setBusiness () {
      let business = this.data.businessType.split(',')
      let newBusiness = []
      this.data.businessList.forEach(item => {
        business.forEach(row => {
          if (item.id === parseInt(row)) newBusiness.push(item)
        })
      })
      this.setData({
        list: newBusiness
      })
    },
    async getBusinessTypeList () {
      try {
        const { ResultSet } = await getBusinessTypeList()
        this.setData({ businessList: ResultSet.userData.businessList.value })
        this.setBusiness()
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    select ({ currentTarget: { dataset: { item } } }) {
      this.setData({ currentItem: item })
    },
    close () {
      this.triggerEvent('close')
    },
    confirm () {
      this.triggerEvent('change', this.data.currentItem)
    }
  },
  ready () {}
})